<?php
/**
* 阿里云对象存储OSS
*/
namespace app\common\model;
use \think\Model;
use OSS\OssClient;
use OSS\Core\OssException;

class OssUp 
{
    private $accessKeyId = 'LTAI4FvY18isnVrSgeXVRogj';
    private $accessKeySecret = 'EqqThYSG0YC7kWXM8lW0OEYs1wzZGy';
    private $endpoint = 'oss-cn-shenzhen.aliyuncs.com';
    private $bucket= "xxzq";
    private $domain_name = 'img.hybrjy.com';
    private $bucket_domain_name = 'xxzq.oss-cn-shenzhen.aliyuncs.com';
    
    //阿里云OSS 云存储
    public function oss_up($file, $object,$type = 1){
        if($type == 1){
            $info = getimagesize($file['tmp_name']);
            $type = explode('/', $info['mime']);
            $type = $type[1];
        }
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        $object .= ".".$type;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        $filePath = $file['tmp_name'];
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            $img = $ret['info']['url'];
            
            $oss_url = $this->bucket_domain_name;
            $img = str_replace($oss_url, $this->domain_name, $img);

            return $img;
        } catch(OssException $e) {
            //var_dump($e->getMessage());die;
            return false;
        }
    }
    
    //阿里云OSS 云存储
    public function oss_up_file($file, $object,$type = 1){
        if($type == 1){
            $info = getimagesize($file['tmp_name']);
            $type = explode('/', $info['mime']);
            $type = $type[1];
        }
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        $object .= ".".$type;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        $filePath = $file['tmp_name'];
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            $img = $ret['info']['url'];
            
            $oss_url = $this->bucket_domain_name;
            $img = str_replace($oss_url, $this->domain_name, $img);

            return $img;
        } catch(OssException $e) {
            //var_dump($e->getMessage());die;
            return false;
        }
    }
    
    //阿里云OSS 云存储
    public function ueditor_oss_up($file, $object,$type = 1){
        if($type == 1){
            $info = getimagesize($file['tmp_name']);
            $type = explode('/', $info['mime']);
            $type = $type[1];
        }

        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        $filePath = $file['tmp_name'];
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            $img = $ret['info']['url'];
            
            $oss_url = $this->bucket_domain_name;
            $img = str_replace($oss_url, $this->domain_name, $img);

            return $img;
        } catch(OssException $e) {
            //var_dump($e->getMessage());die;
            return false;
        }
    }
    
    //阿里云OSS 云存储
    public function oss_catch_up($filePath, $object,$type = 1){
        
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        //$filePath = $file['tmp_name'];
        //var_dump($filePath);die;
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            //var_dump($ret);die;
            $img = $ret['info']['url'];
 
            $oss_url = $this->bucket_domain_name;
            $img = str_replace($oss_url, $this->domain_name, $img);
            //echo "<pre>";
            //echo 1;die;
            return $img;
        } catch(OssException $e) {
            //echo 2;die;
            //var_dump($e->getMessage());die;
            return false;
        }
    }
}
